package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


public class GroupHelper extends HelperWithWebDriverBase{
	
	//private Groups groupCache;
	private Groups groupModel;
	private int lastGroupId;

	public GroupHelper(ApplicationManager manager) {
		super(manager);
		lastGroupId = Integer.parseInt(manager.getHibernateHelper().getLastGroupID());
	}
	
	public Groups getGroups() {
		if (groupModel == null ) {
			groupModel = manager.getHibernateHelper().getGroups();
			//buildGroupCache();
			}
		//groupModel = manager.getHibernateHelper().getGroups();
		return groupModel;
	}
	
	public GroupObject getGroupByName(String GroupName)
	{
		Groups groups = getGroups();
		GroupObject returnedGroup = groups.getGroupByName(GroupName);
		return returnedGroup;
	}
	
	public Groups getUiGroups() {
		HashSet<GroupObject> set = new HashSet<GroupObject>();
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		
		
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			Pattern p = Pattern.compile(".*\\((.*)\\)");
			Matcher m = p.matcher(title);
			if (m.matches())
			{
				title = m.group(1);
			}
		
			GroupObject group = new GroupObject()
					.setName(title).setId(checkbox.getAttribute("value"));
			set.add(group);
			
		}
	
		return new Groups(set);
	}
	
	
	public void createGroup(GroupObject groupObject) {

		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitGroupCreation();
		returnToGroupListPage();
		lastGroupId = lastGroupId + 1;
		groupObject.setId("" + lastGroupId);
		groupModel = getGroups().withAddedWithoutIDClear(groupObject);
	}
	
	public void deleteGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		checkGroup(groupObject);
		click(By.name("delete"));
		returnToGroupListPage();
		groupModel = groupModel.without(groupObject);
		//return targetID;
	}
	 
	public void modifyGroup(GroupObject oldGroupObject, GroupObject newGroupObject){
		manager.getNavigationHelper().goToGroupListPage();
		initGroupModification(oldGroupObject);
		
		fillGroupForm(newGroupObject);
		submitGroupModification();
		returnToGroupListPage();
		groupModel = groupModel.without(oldGroupObject).withAddedWithoutIDClear(newGroupObject);

		
	}
	
	private void checkGroup(GroupObject groupObject) {

		/*String returnId = "";
		
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		int counter = 0;
		for (WebElement checkbox : checkboxes) {
			if (counter == i) {
				click(By.name("selected[]"));
				returnId = checkbox.getAttribute("value");
				break;
			}
			counter ++;
		}
		
	
		return returnId;*/
		
		click(By.xpath("//input[@value='"+groupObject.id+"']"));
		
	}
	private void initGroupCreation() {
		click(By.name("new"));
	}

	private void fillGroupForm(GroupObject groupObject) {
		type("group_name", groupObject.name);
		type("group_header", groupObject.header);
		type("group_footer", groupObject.footer);
	}

	private void submitGroupCreation() {
		click(By.name("submit"));
	}
	
	private void returnToGroupListPage() {
		click(By.linkText("group page"));
	}
	
	private void initGroupModification(GroupObject groupObject) {
		
	//	String returnId = "";

		 checkGroup(groupObject);

		click(By.name("edit"));
		//return returnId;
	}
	
	private void submitGroupModification() {
		click(By.name("update"));
	}
	
	


}
